// 项目信息
function findReports($scope, CertusFormAjax, CertusJsonAjax) {

    var deptIds = "";
    var compIds = "";
    this.getDepts = function () {
        var company = $("#company").val();
        if (company != "") {
            findCompDepts(company, deptIds);
        } else {
            findCompDepts(compIds, deptIds);
        }
    }

    this.findCompDepts = function (company, depts) {
        CertusFormAjax.ajax("findCompDepts", {
            "compCodes": company,
            "deptIds": depts,
            "isBm":"BM"
        }, function (response) {
            var data = response.data;
            $scope.dept = data;
        });
    }

    this.findAllDepts = function (dept) {
        CertusFormAjax.ajax("findQDepts", {
            "deptIds": dept
        }, function (response) {
            var data = response.data;
            $scope.dept = data;
        });
    }
    CertusFormAjax.ajax("isCrcd", {
        "code": "XMJZ"
    }, function (response) {
        var data = JSON.parse(response.data);
        deptIds = data.deptCode;
        compIds = data.comanyCode;
        if (data.comanyCode != "") {
            CertusFormAjax.ajax("companyListByCode", {
                "code": data.comanyCode
            }, function (response) {
                $scope.comp = JSON.parse(response.data);
            });
            findCompDepts(data.comanyCode, deptIds);
        } else {
            $("#company").attr("disabled", "disabled");
            findAllDepts(data.deptCode);
        }

        if (data.isTrue == true) {
            searchs("", "1", deptIds, compIds, getBeforeDate(-2), getBeforeDate(2));
        } else {
            CertusFormAjax.ajax("getDeptCode", {}, function (response) {
                var data1 = response.data;
                $scope.deptCode = data1;
                $("#deptCode").attr("disabled", "disabled");
                $("#deptFlag").html("归属");
                //默认查询
                searchs(data1, "1", deptIds, compIds, getBeforeDate(-8), getBeforeDate(2));
            });
        }
    });

    this.searchs = function (proType, code, deptCode, company, startTime, endTime) {
        //项目进展
        var msgColumns1 = [{
                id: 'pro_name',
                title: '项目名称',
                type: 'string',
                width: 100,
                columnClass: 'text-center',
                resolution: function (value, record) {
                    var content = '';
                    content += '<a href="javascript:findDetail(' + record.pro_id + ');" onclick="listd(this);">' + record.pro_name + '</a>';
                    return content;
                }
            },{
                id: 'contract_name',
                title: '合同名称',
                type: 'string',
                width: 100,
                columnClass: 'text-center',
            }, {
                id: 'content',
                title: '项目进展',
                type: 'string',
                width: 200,
                columnClass: 'text-left',
            }, {
                id: 'next_step',
                title: '下一步计划',
                type: 'string',
                columnClass: 'text-left',
            }, {
                id: 'user_name',
                title: '提交人',
                type: 'string',
                width: 75,
                columnClass: 'text-center',
            }, {
                id: 'code',
                title: '详细状态',
                type: 'string',
                width: 75,
                columnClass: 'text-center',
                resolution: function (value) {
                    var psg = "";
                    if (value == 1) {
                        psg = "线索申请待审批";
                    } else if (value == 2) {
                        psg = "线索审批中";
                    } else if (value == 3) {
                        psg = "线索审批通过";
                    } else if (value == 4) {
                        psg = "线索审批不通过";
                    } else if (value == 5) {
                        psg = "项目立项待审批";
                    } else if (value == 6) {
                        psg = "立项审批中";
                    } else if (value == 7) {
                        psg = "立项审批通过";
                    } else if (value == 8) {
                        psg = "立项审批不通过";
                    } else if (value == 9) {
                        psg = "待商务预测";
                    } else if (value == 10) {
                        psg = "立项通过";
                    } else if (value == 11) {
                        psg = "发起投标待审批";
                    } else if (value == 12) {
                        psg = "投标审批中";
                    } else if (value == 13) {
                        psg = "投标审批通过";
                    } else if (value == 14) {
                        psg = "投标审批不通过";
                    } else if (value == 15) {
                        psg = "合同待审批";
                    } else if (value == 16) {
                        psg = "合同审批中";
                    } else if (value == 17) {
                        psg = "合同审批通过";
                    } else if (value == 18) {
                        psg = "合同审批不通过";
                    } else if (value == 19) {
                        psg = "验收结果上传";
                    } else if (value == 20) {
                        psg = "项目结束";
                    }
                    return psg;
                }
            }
            //     {
            //     id: 'pro_state',
            //     title: '状态',
            //     type: 'string',
            //     width: 80,
            //     codeTable: status,
            //     hideType: 'sm|xs',
            //     columnClass: 'text-center',
            //     resolution: function (value) {
            //         var content = '';
            //         if (value == 1) {
            //             content += '<span style="background:#5bc0de;padding:2px 10px;color:white;">运行中</span>';
            //         }
            //         if (value == 2) {
            //             content += '<span style="background:#d9534f;padding:2px 10px;color:white;">已关闭</span>';
            //         }
            //         if (value == 3) {
            //             content += '<span style="background:#5cb85c;padding:2px 10px;color:white;">已完结</span>';
            //         }
            //         if (value == 4) {
            //             content += '<span style="background:#f8d347;padding:2px 10px;color:white;">已暂停</span>';
            //         }
            //         return content;
            //     }
            // }
            , {
                id: 'pro_lv',
                title: '优先级',
                type: 'string',
                width: 58,
                columnClass: 'text-center',
                resolution: function (value) {
                    var content = '';
                    if (value == 1) {
                        content += '低';
                    }
                    if (value == 2) {
                        content += '中';
                    }
                    if (value == 3) {
                        content += '高';
                    }
                    return content;
                }
            }, {
                id: 'begin_time',
                title: '周期',
                type: 'date',
                format: 'yyyy-MM-dd',
                width: 98,
                otype: 'time_stamp_ms',
                columnClass: 'text-center',
                resolution: function (value, record) {
                    var date1 = new Date(record.begin_time);
                    var content = $.fn.dlshouwen.grid.tools.dateFormat(date1, "yyyy/MM/dd");
                    content += "<br/>至<br/>"
                    var date2 = new Date(record.end_time);
                    content +=  $.fn.dlshouwen.grid.tools.dateFormat(date2, "yyyy/MM/dd");
                    return content;
                }
            }, {
                id: 'create_time',
                title: '提交时间',
                type: 'date',
                width: 98,
                format: 'yyyy/MM/dd',
                otype: 'time_stamp_ms',
                columnClass: 'text-center',
            }, {
                id: 'operation',
                title: '操作',
                width: 60,
                type: 'string',
                columnClass: 'text-center',
                resolution: function (value, record, column, grid, dataNo, columnNo) {
                    var content = '';
                    content += '<button class="btn btn-xs btn-success" onclick="todpxmjz(' + record.id + ');"><i class="fa fa-edit"></i>  点评</button>';
                    content+='<br/><br/>';
                    content += '<button class="btn btn-xs btn-info" onclick="toAddjy(' + record.pro_id + ');"><i class="fa fa-edit"></i>  纪要</button>';
                    return content;
                }
            }
        ];
        $("#msgContainer1").empty();
        $("#msgToolBarContainer1").empty();
        CertusJsonAjax.dataInit("findProReport", {
            "proType": proType,
            "code": code,
            "deptCode": deptCode,
            "compCode": company,
            "startTime": startTime,
            "endTime": endTime
        }, msgColumns1, 'msgContainer1', 'msgToolBarContainer1', 100, [50, 100, 150, 200, 250]);

        //项目动态
        var msgColumns = [{
            id: 'pro_name',
            title: '项目名称',
            type: 'string',
            width: 350,
            columnClass: 'text-center',
            resolution: function (value, record) {
                var content = '';
                content += '<a href="javascript:findDetail(' + record.pro_id + ');"  onclick="listd(this);">' + record.pro_name + '</a>';
                return content;
            }
        }, {
            id: 'content',
            title: '具体内容',
            type: 'string',
            columnClass: 'text-left',
        }, {
            id: 'user_name',
            title: '提交人',
            type: 'string',
            width: 75,
            columnClass: 'text-center',
        }, {
            id: 'create_time',
            title: '创建时间',
            type: 'date',
            width: 100,
            format: 'yyyy-MM-dd',
            otype: 'time_stamp_ms',
            columnClass: 'text-center',
        },{
            id: 'operation',
            title: '操作',
            width: 100,
            type: 'string',
            columnClass: 'text-center',
            resolution: function (value, record) {
                var content = '';
                //查看流程变更详情，其他则没有操作
                if (record.step_id!=undefined&&record.step_id!=null&&record.step_id!=''){
                    content = '<button class="btn btn-xs btn-success" onclick="ckDetail(\'' + record.step_id + '\');"><i class="fa fa-edit"></i>  查看详情</button>';
                }else{
                    content="";
                }
                return content;
            }
        }];
        $("#msgContainer").empty();
        $("#msgToolBarContainer").empty();
        CertusJsonAjax.dataInit("findAllProDynas", {
            "proType": proType,
            "code": code,
            "deptCode": deptCode,
            "compCode": company,
            "startTime": startTime,
            "endTime": endTime
        }, msgColumns, 'msgContainer', 'msgToolBarContainer', 5);

        //项目风险和问题
        var msgColumns2 = [{
            id: 'operation',
            title: '项目名称',
            type: 'string',
            width: 150,
            columnClass: 'text-center',
            resolution: function (value, record) {
                var content = '';
                content += '<a href="javascript:findDetail(' + record.pro_id + ');" onclick="listd(this);">' + record.pro_name + '</a>';
                return content;
            }
        }, {
            id: 'risk_content',
            title: '风险和问题',
            type: 'string',
            width: 250,
            columnClass: 'text-left',
        }, {
            id: 'risk_manage',
            title: '预规避和处理措施',
            type: 'string',
            columnClass: 'text-left',
        }, {
            id: 'risk_lv',
            title: '风险级别',
            type: 'string',
            width: 75,
            columnClass: 'text-center',
        }, {
            id: 'user_name',
            title: '提交人',
            type: 'string',
            width: 90,
            columnClass: 'text-center',
        }, {
            id: 'create_time',
            title: '创建时间',
            type: 'date',
            width: 100,
            format: 'yyyy-MM-dd',
            otype: 'time_stamp_ms',
            columnClass: 'text-center',
        }];
        $("#msgContainer2").empty();
        $("#msgToolBarContainer2").empty();
        CertusJsonAjax.dataInit("findProRisks", {
            "proType": proType,
            "code": code,
            "deptCode": deptCode,
            "compCode": company,
            "startTime": startTime,
            "endTime": endTime
        }, msgColumns2, 'msgContainer2', 'msgToolBarContainer2');

        //项目决策
        var msgColumns3 = [{
            id: 'operation',
            title: '项目名称',
            type: 'string',
            width: 150,
            columnClass: 'text-center',
            resolution: function (value, record) {
                var content = '';
                content += '<a href="javascript:findDetail(' + record.pro_id + ');" onclick="listd(this);">' + record.pro_name + '</a>';
                return content;
            }
        }, {
            id: 'decision_content',
            title: '内容',
            type: 'string',
            width: 250,
            columnClass: 'text-left',
        }, {
            id: 'decision_manage',
            title: '结论',
            type: 'string',
            columnClass: 'text-left',
        }, {
            id: 'user_name',
            title: '提交人',
            type: 'string',
            width: 75,
            columnClass: 'text-center',
        }, {
            id: 'create_time',
            title: '创建时间',
            type: 'date',
            width: 100,
            format: 'yyyy-MM-dd',
            otype: 'time_stamp_ms',
            columnClass: 'text-center',
        }];
        $("#msgContainer3").empty();
        $("#msgToolBarContainer3").empty();
        CertusJsonAjax.dataInit("findProDecisions", {
            "proType": proType,
            "code": code,
            "deptCode": deptCode,
            "compCode": company,
            "startTime": startTime,
            "endTime": endTime
        }, msgColumns3, 'msgContainer3', 'msgToolBarContainer3');

        //未填写进展的项目列表
        var msgColumns4 = [{
            id: 'operation',
            title: '项目名称',
            type: 'string',
            columnClass: 'text-center',
            resolution: function (value, record) {
                var content = '';
                content += '<a href="javascript:findDetail(' + record.id + ');" onclick="listd(this);">' + record.pro_name + '</a>';
                return content;
            }
        }, {
            id: 'pro_stage',
            title: '项目阶段',
            type: 'string',
            width: 150,
            columnClass: 'text-center',
        }, {
            id: 'department_name',
            title: '归属部门',
            type: 'string',
            width: 200,
            columnClass: 'text-center',
        }, {
            id: 'mgUser',
            title: '项目负责人',
            type: 'string',
            width: 100,
            columnClass: 'text-center',
        }, {
            id: 'xsUser',
            title: '线索负责人',
            type: 'string',
            width: 100,
            columnClass: 'text-center',
        }, {
            id: 'cpUser',
            title: '产品经理',
            type: 'string',
            width: 75,
            columnClass: 'text-center',
        }, {
            id: 'jfUser',
            title: '交付经理',
            type: 'string',
            width: 75,
            columnClass: 'text-center',
        }, {
            id: 'sUser',
            title: '销售经理',
            type: 'string',
            width: 75,
            columnClass: 'text-center',
        }];
        $("#msgContainer4").empty();
        $("#msgToolBarContainer4").empty();
        CertusJsonAjax.dataInit("findNoPros", {
            "proType": proType,
            "deptCode": deptCode,
            "code": "1",
            "compCode": company,
            "startTime": startTime,
            "endTime": endTime
        }, msgColumns4, 'msgContainer4', 'msgToolBarContainer4');
    }
    this.findDetail = function (proId,e) {
        $scope.proId = proId;
        probox($scope, CertusFormAjax);
    }

    this.listd = function (e) {
        $(e).addClass("getCild")
    };

    $(".searchs").on('click', function () {
        var startTime = $("#datetime-star").val();
        var endTime = $("#datetime-2").val();
        var proType = $("#proType").val();
        var deptCode = $("#deptCode").val();
        var code = $("#code").val();
        if (code == "2" || code == "3") {
            if (deptCode == "") {
                layer.msg("请选择一个部门");
                return;
            }
        } else {
            if (deptCode == "") {
                deptCode = deptIds;
            }
        }
        var company = $("#company").val();
        if (company == "") {
            company = compIds;
        }
        if (deptCode != "") {
            $("#deptFlag").html("归属");
        } else {
            $("#deptFlag").html("所有");
        }
        searchs(proType, code, deptCode, company, startTime, endTime);
    });
    $("#datetime-star").val(getBeforeDate(-2));
    $("#datetime-2").val(getBeforeDate(2));
}
//n为你要传入的参数，当前为0，前一天为-1，后一天为1
function getBeforeDate(n) {
    var date = new Date();
    var year, month, day;
    date.setDate(date.getDate() + n);
    year = date.getFullYear();
    month = date.getMonth() + 1;
    day = date.getDate();
    s = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (day < 10 ? ('0' + day) : day);
    return s;
}

function ckDetail(stepId) {
    window.location.href="#/toFindModifyDetail/"+stepId;
}

function todpxmjz(rid) {
    location.href = "#/toProJzdp/" + rid;
}

function toAddjy(proId) {
    location.href = "#/toAddjy/" + proId;
}